//
//  SWCenterController.swift
//  SinaWeibo
//
//  Created by wszyxc on 16/7/26.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

import UIKit

class SWCenterController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        setupNav()
    }
    
    private func setupNav(){
    
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: nil, title: "取消", target: self, action: "gobackClick")
        
        
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: nil, title: "发布", target: self, action: "publishClick")
        
        navigationItem.rightBarButtonItem?.enabled = false

    }
    
    @objc private func publishClick(){
        
        print("发布")
        
    }
    
   @objc private func gobackClick(){
        
        
//        navigationController?.dismissViewControllerAnimated(true, completion: { () -> Void in
//            
//        })
    
        dismissViewControllerAnimated(true) { () -> Void in
            
        }
    }
    
    //添加控件
    private func setupUI(){
        
        
        self.view.backgroundColor = UIColor.whiteColor()
        //添加textView
        view.addSubview(textView)
        
        textView.placeHolder.text = "啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"
        
        textView.delegate = self
        
        //添加约束
        
        textView.snp_makeConstraints { (make) -> Void in
            
            
            make.edges.equalTo(self.view).offset(UIEdgeInsetsZero)
        }
        
    }
    
    //懒加载textView
    
    private lazy var textView:SWTextView = SWTextView()
    

}



extension SWCenterController:UITextViewDelegate{
    
    
    func textViewDidChange(textView: UITextView) {
        //文字有变动就进来
        
        self.navigationItem.rightBarButtonItem?.enabled = textView.hasText()
        
        self.textView.placeHolder.hidden = textView.hasText()
        
    }
    
    //键盘回退
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        self.view.endEditing(true)
        
    }
    
}



